﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="landing.aspx.cs" Inherits="GT5RemoteMobile.landing" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link rel="stylesheet" href="Styles/jquery.mobile-1.0a3.min.css" />
    <script type="text/javascript" src="Scripts/jquery-1.5.1.min.js"></script>
    <script type="text/javascript" src="Scripts/jquery.mobile-1.0a3.min.js"></script>
    <script type="text/javascript" src="Scripts/writeCapture.js"></script>
    <script type="text/javascript" src="Scripts/jquery.writeCapture.js"></script>
    <script type="text/javascript">
        $(document).bind('mobileinit', function () {
            $.mobile.ajaxEnabled = false;
        });
        function gup(name) {
            name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
            var regexS = "[\\?&]" + name + "=([^&#]*)";
            var regex = new RegExp(regexS);
            var results = regex.exec(window.location.href);
            if (results == null)
                return "";
            else
                return results[1];
        }
        $.writeCapture.autoAsync();
    </script>
    <%if (Resources.Settings.EnableGoogleApps == "true")
      { %>
    <script type="text/javascript">

        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-21834495-2']);
        _gaq.push(['_trackPageview']);

        (function () {
            var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
            ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
        })();

    </script>
    <%} %>
</head>
<body>
    <form id="form1" runat="server">
    <div id="gt5-home" data-role="page" data-theme="a">
        <div id="gt5-homeheader" data-role="header">
            <h1>GT5 Mobile</h1>
        </div>
        <div data-role="content">
            <ul data-role="listview" data-inset="true" data-theme="a" data-dividertheme="a">
                <li data-role="list-divider" id="userId" runat="server"></li>
                <li><asp:LinkButton runat="server" ID="newRaceButton" OnClick="StartNewRace" Text="Start a New Race"></asp:LinkButton></li>
                <li><a href="friends.aspx" data-ajax="false">Friends List</a><span class="ui-li-count"><%=psnProfile.Friend_Count%></span></li>
            </ul>
        </div>
        <div data-role="footer">
        <%if (Resources.Settings.EnableGoogleApps == "true")
          { %>
            <%
  // AdMob Publisher Code
  // Language: C# (.NET 2.0 or above)
  // Version: 20081105
  // Copyright AdMob, Inc., All rights reserved
  // Documentation at http://developer.admob.com/wiki/Main_Page
  
  Hashtable admobRequiredParams = new Hashtable();
  Hashtable admobOptionalParams = new Hashtable();
  
  admobRequiredParams.Add("PUBLISHER_ID", "a14d8403242d36a"); // Required to request ads. To find your Publisher ID, log in to your AdMob account and click on the "Sites & Apps" tab.
  admobRequiredParams.Add("ANALYTICS_ID", "your_analytics_site_id"); // Required to collect Analytics data. To find your Analytics ID, log in to your Analytics account and click on the "Edit" link next to the name of your site.
  admobRequiredParams.Add("COOKIE_DOMAIN", ""); // If your mobile site uses multiple subdomains, replace "" with your root domain (e.g. "example.com") to make the AdMob cookie visible across subdomains.
  admobRequiredParams.Add("AD_REQUEST", true);  // To request an ad, set to true.
  admobRequiredParams.Add("ANALYTICS_REQUEST", false);  // To enable the collection of analytics data, set to true.
  admobRequiredParams.Add("TEST_MODE", false); // While testing, set to true. When you are ready to make live requests, set to false.
  // Additional optional parameters are available at: http://developer.admob.com/wiki/AdCodeDocumentation
  //admobOptionalParams.Add("title", "Insert Page Title Here");  // Analytics allows you to track site usage based on custom page titles. Enter custom title in this parameter.
  //admobOptionalParams.Add("event", "Insert Event Name Here");  // To learn more about events, log in to your Analytics account and visit this page: http://analytics.admob.com/reports/events/add

  // Send request to AdMob. To make additional ad requests per page, copy and paste this function call elsewhere on your page.
  Response.Write(AdmobRequest(admobRequiredParams, admobOptionalParams));
  
  /////////////////////////////////
  // Do not edit below this line //
  /////////////////////////////////
%>

<script runat="server">
  // This section defines AdMob functions and should be used AS IS.
  // We recommend placing the following code in a separate file that is imported where needed. 
  
  bool admobPixelSent = false;

  string AdmobRequest(Hashtable requiredParams, Hashtable optionalParams)
  {
    if (requiredParams == null) return "";

    bool adMode = false;
    if (requiredParams.ContainsKey("PUBLISHER_ID") && requiredParams.ContainsKey("AD_REQUEST"))
      adMode = (bool)requiredParams["AD_REQUEST"];

    bool analyticsMode = false;
    if (requiredParams.ContainsKey("ANALYTICS_ID") && requiredParams.ContainsKey("ANALYTICS_REQUEST") && !admobPixelSent)
      analyticsMode = (bool)requiredParams["ANALYTICS_REQUEST"];

    String rt = adMode ? (analyticsMode ? "2" : "0") : (analyticsMode ? "1" : "-1");
    if (rt == "-1") return "";

    Session["admobDummy"] = 1; // store something in the session so that it persists
    String protocol = Request.IsSecureConnection ? "https" : "http";
    String domain = requiredParams.ContainsKey("COOKIE_DOMAIN") ? (string)requiredParams["COOKIE_DOMAIN"] : "";
    String z = (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds.ToString();
    String o = AdmobGetCookieValue(domain);
    StringBuilder admobContents = new StringBuilder();
    AdmobAppendParams(admobContents, "rt", rt);
    AdmobAppendParams(admobContents, "z", z);
    AdmobAppendParams(admobContents, "u", Request.UserAgent);
    AdmobAppendParams(admobContents, "i", Request.UserHostAddress);
    AdmobAppendParams(admobContents, "p", Request.Url.ToString());
    AdmobAppendParams(admobContents, "t", AdmobMd5(Session.SessionID));
    AdmobAppendParams(admobContents, "v", "20081105-CSHARP-daf93db369888d5f");
    AdmobAppendParams(admobContents, "o", o);
    if (adMode) AdmobAppendParams(admobContents, "s", (string)requiredParams["PUBLISHER_ID"]);
    if (analyticsMode) AdmobAppendParams(admobContents, "a", (string)requiredParams["ANALYTICS_ID"]);
    
    if (optionalParams != null)
      foreach (string optkey in optionalParams.Keys)
        AdmobAppendParams(admobContents, optkey, (string)optionalParams[optkey]);

    ArrayList ignoreHeaders = ArrayList.Adapter(new String[] { "PRAGMA", "CACHE-CONTROL", "CONNECTION", "USER-AGENT", "COOKIE" });
    foreach (string name in Request.Headers)
      if (!ignoreHeaders.Contains(name.ToUpper()))
        AdmobAppendParams(admobContents, "h[" + name + "]", string.Join(",", Request.Headers.GetValues(name)));

    if (requiredParams.ContainsKey("TEST_MODE") && (bool)requiredParams["TEST_MODE"]) admobContents.Append("&m=test");
    
    StringBuilder admobReturn = new StringBuilder();
    const int ADMOB_TIMEOUT = 1000; // 1 second timeout
    System.Net.HttpWebResponse admobWebResponse = null;
    System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
    try
    {
      byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(admobContents.ToString());
      System.Net.HttpWebRequest admobWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://r.admob.com/ad_source.php");
      admobWebRequest.Method = "POST";
      admobWebRequest.ContentType = "application/x-www-form-urlencoded";
      admobWebRequest.ContentLength = postBytes.Length;
      admobWebRequest.Timeout = ADMOB_TIMEOUT;
      admobWebRequest.ServicePoint.Expect100Continue = false;
      sw = System.Diagnostics.Stopwatch.StartNew();
      System.IO.Stream admobWriter = admobWebRequest.GetRequestStream();
      admobWriter.Write(postBytes, 0, postBytes.Length);
      admobWriter.Close();
      admobWebResponse = (System.Net.HttpWebResponse)admobWebRequest.GetResponse();
      sw.Stop();
      System.IO.StreamReader admobReader = new System.IO.StreamReader(admobWebResponse.GetResponseStream());
      admobReturn.Append(admobReader.ReadToEnd().Trim());
      admobReader.Close();
    }
    catch (Exception e) { }
    finally { if (admobWebResponse != null) admobWebResponse.Close(); }
    sw.Stop();
    
    if (!admobPixelSent)
    {
      admobPixelSent = true;
      admobReturn.Append("<img src=\"" + protocol + "://p.admob.com/e0?")
        .Append("rt=").Append(rt)
        .Append("&amp;z=").Append(z)
        .Append("&amp;a=").Append(analyticsMode ? requiredParams["ANALYTICS_ID"] : "")
        .Append("&amp;s=").Append(adMode ? requiredParams["PUBLISHER_ID"] : "")
        .Append("&amp;o=").Append(o)
        .Append("&amp;lt=").Append(sw.Elapsed.TotalSeconds.ToString())
        .Append("&amp;to=").Append(ADMOB_TIMEOUT / 1000.0)
        .Append("\" alt=\"\" width=\"1\" height=\"1\"/>");
    }
    
    return admobReturn.ToString();
  }
  
  string AdmobGetCookieValue(String domain)
  { 
    if (Request.Cookies["admobuu"] != null)
      return Request.Cookies["admobuu"].Value;

    Response.Cookies["admobuu"].Value = System.Guid.NewGuid().ToString().Replace("-", "");
    Response.Cookies["admobuu"].Expires = DateTime.Now.AddYears(20);
    Response.Cookies["admobuu"].Path = "/";
    if (domain.Length > 0)
    {
      if (domain[0] != '.') domain = "." + domain;
      Response.Cookies["admobuu"].Domain = domain;
    }
    return Request.Cookies["admobuu"].Value; // the new cookie is immediately available in Request.Cookies
  }
  
  void AdmobAppendParams(StringBuilder contents, String key, String val)
  {
    if (!string.IsNullOrEmpty(val))
    {
      if (contents.Length > 0) contents.Append("&");
      contents.Append(HttpUtility.UrlEncode(key, Encoding.UTF8)).Append("=").Append(HttpUtility.UrlEncode(val, Encoding.UTF8));
    }
  }

  string AdmobMd5(string val)
  {
    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
    System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
    byte[] hashBytes = md5.ComputeHash(encoding.GetBytes(val));
    StringBuilder md5Val = new StringBuilder();
    foreach (byte b in hashBytes)
    {
      md5Val.Append(String.Format("{0:x2}", b));
    }
    return md5Val.ToString();
  }
</script>
            <%} %>
        </div>
    </div>
    </form>
</body>
</html>
